package com.gxa.sso.controller;

import com.gxa.sso.entity.User;
import com.gxa.sso.exception.BusinessException;
import com.gxa.sso.service.UserService;
import com.gxa.sso.utils.JsonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping(value = "/login")
    public JsonResult login(@RequestParam("phone") String phone, @RequestParam("code") String code) throws BusinessException {
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            throw new BusinessException("手机号码格式不合法");
        }
        JsonResult jsonResult=new JsonResult();
        Map<String, String> login = userService.login(phone, code);
        jsonResult.setValue("token",login.get("token"));

        return  jsonResult;
    }

//    @RequestMapping(value = "/valuedateToken")
//    public JsonResult valuedateToken(@RequestParam("token") String token) throws BusinessException {
//        User user=userService.queryUserByToken(token);
//        JsonResult jsonResult=new JsonResult();
//        jsonResult.setData(user);
//        jsonResult.setCode("1");
//        return jsonResult;
//    }
    @RequestMapping(value = "/valuedateToken")
    public User valuedateToken(@RequestParam("token") String token) throws BusinessException {
        User user=userService.queryUserByToken(token);

        return user;
    }
}
